//
//  GDListView.m
//  Gunterapple
//
//  Created by 热带雨林 on 2021/1/30.
//  Copyright © 2021 热带雨林. All rights reserved.
//
#import "GDListView.h"
#import "GDListViewTableViewCell.h"
#import "viewModel.h"
static NSString *cellID = @"cellID";
@interface GDListView ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableViwe;
@property (nonatomic, strong) NSArray *dataArray;
@end

@implementation GDListView

- (instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        [self setUpUI];
        [self loadData];
    }
    return self;
}
- (void)setUpUI{
    self.backgroundColor = [UIColor redColor];
    [self addSubview:self.tableViwe];
    [self.tableViwe registerClass:[GDListViewTableViewCell class] forCellReuseIdentifier:cellID];
    [self.tableViwe mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.height.equalTo(self);
    }];
}
- (void)loadData{
    self.dataArray = [NSArray gg_arrayModelWithPlistName:@"viewCellDataList.plist" withClassName:@"viewModel"];
    [self.tableViwe reloadData];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 17;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor blackColor];
    UILabel *title = [[UILabel alloc]init];
    title.text = @"FMDB";
    title.textColor = [UIColor whiteColor];
    [view addSubview:title];
    title.font = [UIFont systemFontOfSize:8];
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(view);
        make.left.equalTo(view).offset(15);
    }];
    
    return view;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GDListViewTableViewCell *cell = [[GDListViewTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.dataArray[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if([self.delgate respondsToSelector:@selector(callBackWithindexPath:)]){
        [self.delgate callBackWithindexPath:indexPath.row];
    }
}
- (UITableView *)tableViwe{
    if(!_tableViwe){
        _tableViwe = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableViwe.bounces = NO;
        _tableViwe.showsVerticalScrollIndicator = NO;
        _tableViwe.separatorStyle = UITableViewCellSelectionStyleNone;
        _tableViwe.dataSource = self;
        _tableViwe.delegate = self;
        _tableViwe.rowHeight = 60;
    }
    return _tableViwe;
}
@end
